package com.yjd.pub.system.service;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.yjd.comm.vo.Constant;
import org.springframework.stereotype.Service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.comm.system.dto.OperParamDto;
import com.yjd.comm.system.model.SysParamModel;
import com.yjd.comm.system.service.ISysParamService;
import com.yjd.comm.util.FrameUtil;
import com.yjd.pub.system.dao.ISysParamDao;

/**
 * <b>description</b>：系统菜单业务实现<br>
 * <b>time</b>：2014-11-06 17:18:10 <br>
 * <b>author</b>： lindy lindytem@126.com
 */
@Service("sysParamService")
public class SysParamServiceImpl extends BaseServiceImpl implements
		ISysParamService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public SysParamModel insert(SysParamModel model, OperParamDto operParamDto)
			throws Exception {
		this.adminLog(operParamDto, "添加系统参数[" + FrameUtil.json(operParamDto)
				+ "]", FrameUtil.getTime(null));
		return this.sysParamDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(SysParamModel model, OperParamDto operParamDto)
			throws Exception {
		this.adminLog(operParamDto, "更新系统参数[" + FrameUtil.json(operParamDto)
				+ "]", FrameUtil.getTime(null));
		return this.sysParamDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id_param
	 * @return
	 * @throws Exception
	 */
	public int delete(Long id_param, OperParamDto operParamDto)
			throws Exception {
		if (id_param == null) {
			FrameUtil.throwBaseException1("paramEx.00001");
		}
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id_param", id_param);
		SysParamModel model = this.getModelById(id_param,DbWREnums.WRITE);
		if (model != null) {
			this.adminLog(operParamDto,
					"删除系统参数[" + FrameUtil.json(model) + "]",
					FrameUtil.getTime(null));
		} else {
			FrameUtil.throwBaseException1("normal.dataNotExists");
		}
		return this.sysParamDao.deleteModel(paramMap, true);
	}

	public SysParamModel getModelByKey(Constant.SysParamKeyEnum sysParamKeyEnum) throws Exception {
		if(sysParamKeyEnum!=null) {
			return this.getModelOne(FrameUtil.newHashMap("param_key", sysParamKeyEnum.getValue()), DbWREnums.READ);
		}else{
			return null;
		}
	}
	
	
	@Override
	public int updateByMap(Map<Object, Object> param, OperParamDto operParamDto) throws Exception {
		this.adminLog(operParamDto, "更新系统参数[" + FrameUtil.json(param)
				+ "]", FrameUtil.getTime(null));
		return this.sysParamDao.updateByMap(param, true);
	}




	@Resource
	private ISysParamDao sysParamDao;

	@Override
	public IBaseDao getBaseDao() {
		return this.sysParamDao;
	}

}
